package com.runfast.waimai.web.controller;

import com.runfast.common.utils.SessionContext;
import com.runfast.common.web.entity.Result;
import com.runfast.waimai.dao.model.*;
import com.runfast.waimai.service.WaiMaiRunfastAgentbusinessService;
import com.runfast.waimai.service.WaiMaiRunfastDeliverCostService;
import com.runfast.waimai.utils.TimeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.*;

/**
 * ${DESCRIPTION}  配送费用设置
 *
 * @author chenzheng
 * @create 2018-05-21 15:50
 **/
@RestController("deliverCostController")
@RequestMapping("/server/deliverCost")
public class DeliverCostController {
    @Autowired
    private WaiMaiRunfastDeliverCostService waiMaiRunfastDeliverCostService;
    @Autowired
    private WaiMaiRunfastAgentbusinessService waiMaiRunfastAgentbusinessService;
    /**
     * 配送费用设置列表带分页的
     */
    @GetMapping("/list")
    public Result list(@PageableDefault Pageable pageable,Integer agentid){
        BaseUser admin = SessionContext.getAdmin();
        WaiMaiRunfastAgentaccounts agentAccounts= SessionContext.getCurrentRunfastAgentaccounts();
        WaiMaiRunfastDeliverCostExample example=new WaiMaiRunfastDeliverCostExample();
        WaiMaiRunfastDeliverCostExample.Criteria exampleCriteria=example.createCriteria();
        if(admin!=null && admin.getType()==2 && agentAccounts!=null){
            //代理商
            if(agentid!=null){
                if(agentid.equals(agentAccounts.getAgentid())){
                    exampleCriteria.andAgentidEqualTo(agentid);
                }else{return Result.fail(null,"只能查看自己的配送信息！");}
            }else{exampleCriteria.andAgentidEqualTo(agentAccounts.getAgentid());}
        }else if(admin!=null && admin.getType()==1){
            //后台
            if(agentid!=null){
                WaiMaiRunfastAgentbusiness   agentbusiness1=waiMaiRunfastAgentbusinessService.selectByPrimaryKey(agentid);
                if(agentbusiness1!=null){
                    exampleCriteria.andAgentidEqualTo(agentbusiness1.getId());
                }else{return Result.fail(null,"找不到对应的代理商！");}
            }
        }
        List<WaiMaiRunfastDeliverCost> list=waiMaiRunfastDeliverCostService.selectByExampleWithPageable(example,pageable);
        Integer total=waiMaiRunfastDeliverCostService.countByExample(example);
        Map<Object,Object> map=new HashMap<Object, Object>();
        map.put("list",list);
        map.put("total",total);
        return Result.ok(null,map);
    }

    /**
     * 配送费用设置列表
     */
   /* @GetMapping("/select")
    //@RequiresPermissions("server:goodsSellType:select")
    public Result select(){
        List<RunfastGoodsSellType> list = runfastGoodsSellTypeService.selectByExample(null);
        return Result.ok(null,list);
    }*/


     /**配送模板详情*/
     @GetMapping("/info")
     public Result info(WaiMaiRunfastDeliverCost deliverCost1) throws ParseException{
         WaiMaiRunfastDeliverCost deliverCost=new WaiMaiRunfastDeliverCost();
         if(deliverCost1!=null && deliverCost1.getId()!=null){
             deliverCost=waiMaiRunfastDeliverCostService.selectByPrimaryKey(deliverCost1.getId());
             setDeliverCost1(deliverCost);
             if(deliverCost==null){
                 return Result.fail(null,"找不到对应的配送信息！");
             }
         }else{return Result.fail(null,"传入参数不对！");}
         return Result.ok(null,deliverCost);
     }
    public  void setDeliverCost1(WaiMaiRunfastDeliverCost deliverCost) throws ParseException {
        if(deliverCost.getStarttimeday1()!=null && deliverCost.getEndtimeday1()!=null){
            deliverCost.setFirstTime(TimeUtil.getHourAndMinuteToString(deliverCost.getStarttimeday1())+","+TimeUtil.getHourAndMinuteToString(deliverCost.getEndtimeday1()));
        }
        if(deliverCost.getStarttimenight2()!=null && deliverCost.getEndtimenight2()!=null){
            deliverCost.setSecondTime(TimeUtil.getHourAndMinuteToString(deliverCost.getStarttimenight2())+","+TimeUtil.getHourAndMinuteToString(deliverCost.getEndtimenight2()));
        }
    }

    /**
     * 保存配送费用设置
     */
    @PostMapping("/save")
    public Result save(@RequestBody WaiMaiRunfastDeliverCost deliverCost) throws ParseException{
         if(deliverCost!=null && deliverCost.getAgentid()!=null && StringUtils.isNotBlank(deliverCost.getFirstTime()) &&StringUtils.isNotBlank(deliverCost.getDistrange()) && deliverCost.getTimecost1()!=null && deliverCost.getCharge1()!=null
                 &&deliverCost.getMaxcharge()!=null && deliverCost.getNocharge()!=null && deliverCost.getSpeed()!=null && deliverCost.getUpdatehour()!=null && deliverCost.getMaxdistance()!=null ){
             setDeliverCost(deliverCost);
             WaiMaiRunfastAgentbusiness  agentbusiness=waiMaiRunfastAgentbusinessService.selectByPrimaryKey(deliverCost.getAgentid());
             if(agentbusiness!=null){
                 deliverCost.setAgentid(agentbusiness.getId());
                 deliverCost.setAgentname(agentbusiness.getName());
             }else{return Result.fail(null,"找不到对应的代理商！");}
             deliverCost.setCreatetime(new Date());
             waiMaiRunfastDeliverCostService.saveFor(deliverCost);
         }else{return Result.fail(null,"传入参数不对！");}
        return Result.ok(null);
    }
    public  void setDeliverCost(WaiMaiRunfastDeliverCost deliverCost) throws ParseException {
        if(StringUtils.isNoneBlank(deliverCost.getFirstTime())){
            List<String> result = Arrays.asList(StringUtils.split(deliverCost.getFirstTime(),","));
            if(result.size()>0){
                deliverCost.setStarttimeday1(TimeUtil.getHourAndMinute(result.get(0)));
                deliverCost.setEndtimeday1(TimeUtil.getHourAndMinute(result.get(1)));
            }
        }else{
            deliverCost.setStarttimeday1(null);
            deliverCost.setEndtimeday1(null);
        }
        if(StringUtils.isNoneBlank(deliverCost.getSecondTime())){
            List<String> result = Arrays.asList(StringUtils.split(deliverCost.getSecondTime(),","));
            if(result.size()>0){
                deliverCost.setStarttimenight2(TimeUtil.getHourAndMinute(result.get(0)));
                deliverCost.setEndtimenight2(TimeUtil.getHourAndMinute(result.get(1)));
            }
        }else{
            deliverCost.setStarttimenight2(null);
            deliverCost.setEndtimenight2(null);
        }

    }
    /**
     * 修改配送费用设置
     */
    @PostMapping("/update")
    public Result update(@RequestBody WaiMaiRunfastDeliverCost deliverCost) throws ParseException {
        if(deliverCost!=null && deliverCost.getId()!=null && deliverCost.getAgentid()!=null && StringUtils.isNotBlank(deliverCost.getFirstTime()) &&StringUtils.isNotBlank(deliverCost.getDistrange()) && deliverCost.getTimecost1()!=null && deliverCost.getCharge1()!=null
                &&deliverCost.getMaxcharge()!=null && deliverCost.getNocharge()!=null && deliverCost.getSpeed()!=null && deliverCost.getUpdatehour()!=null && deliverCost.getMaxdistance()!=null ){
            setDeliverCost(deliverCost);
            WaiMaiRunfastDeliverCost deliverCost1=waiMaiRunfastDeliverCostService.selectByPrimaryKey(deliverCost.getId());
            if(deliverCost1!=null){
                WaiMaiRunfastAgentbusiness  agentbusiness=waiMaiRunfastAgentbusinessService.selectByPrimaryKey(deliverCost.getAgentid());
                if(agentbusiness!=null){
                   if(agentbusiness.getId().equals(deliverCost1.getAgentid())){
                       deliverCost1.setAgentname(agentbusiness.getName());
                       deliverCost1.setAgentid(agentbusiness.getId());
                       deliverCost1.setEndtimenight2(deliverCost.getEndtimenight2());
                       deliverCost1.setStarttimenight2(deliverCost.getStarttimenight2());
                       deliverCost1.setEndtimeday1(deliverCost.getEndtimeday1());
                       deliverCost1.setStarttimeday1(deliverCost.getStarttimeday1());
                       deliverCost1.setCharge1(deliverCost.getCharge1());
                       deliverCost1.setCharge2(deliverCost.getCharge2());
                       deliverCost1.setDistrange(deliverCost.getDistrange());
                       deliverCost1.setMaxcharge(deliverCost.getMaxcharge());
                       deliverCost1.setSpeed(deliverCost.getSpeed());
                       deliverCost1.setTimecost1(deliverCost.getTimecost1());
                       deliverCost1.setTimecost2(deliverCost.getTimecost2());
                       deliverCost1.setUpdatehour(deliverCost.getUpdatehour());
                       deliverCost1.setDistrange(deliverCost.getDistrange());
                       deliverCost1.setMaxdistance(deliverCost.getMaxdistance());
                       waiMaiRunfastDeliverCostService.updateByPrimaryKeyWithBLOBs(deliverCost1);
                   }else{return Result.fail(null,"只能修改对应代理商的配送模板！");}
                }else{return Result.fail(null,"找不到对应的代理商！");}
            }else{return Result.fail(null,"找不到对应的配送模板！");}
        }
        return Result.ok(null);
    }

    /**
     * 删除配送费用设置
     */
    @GetMapping("/delete")
    public Result delete(Integer id){
        if(id!=null){
            waiMaiRunfastDeliverCostService.deleteByPrimaryKey(id);
        }else{return Result.fail(null,"传入参数不对！");}
        return Result.ok(null);
    }

    /**设置为默认*/
    @GetMapping("/setToDefault")
    public Result setToDefault(WaiMaiRunfastDeliverCost deliverCost){
        if(deliverCost!=null && deliverCost.getId()!=null && deliverCost.getIsdefault()!=null){
            WaiMaiRunfastDeliverCost deliverCost1=waiMaiRunfastDeliverCostService.selectByPrimaryKey(deliverCost.getId());
            if(deliverCost1!=null){
                if(deliverCost1.getAgentid()!=null){
                    WaiMaiRunfastDeliverCostExample example=new WaiMaiRunfastDeliverCostExample();
                    example.createCriteria().andAgentidEqualTo(deliverCost1.getAgentid()).andIsdefaultEqualTo(1);
                    List<WaiMaiRunfastDeliverCost> deliverCostList=waiMaiRunfastDeliverCostService.selectByExample(example);
                    if(deliverCostList.size()>0){
                         for(WaiMaiRunfastDeliverCost deliverCost2:deliverCostList){
                             deliverCost2.setIsdefault(0);
                             waiMaiRunfastDeliverCostService.updateByPrimaryKeySelective(deliverCost2);
                         }
                    }
                    deliverCost1.setIsdefault(1);
                    waiMaiRunfastDeliverCostService.updateByPrimaryKeySelective(deliverCost1);
                }
            }else{return Result.fail(null,"未找到对应的配送模板！");}
        }else{return Result.fail(null,"传入参数不对！");}
        return Result.ok(null);
    }

}
